package com.bstek.demo.dubbo.controller;

import com.bstek.demo.dubbo.service.HealthService;
import com.bstek.frame.model.ApiResult;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @author: hans
 * @date: 2025/7/17 11:35
 * Description:
 */
@RestController
@RequestMapping("/dubbo/client")
public class HealthCheckController {

    @DubboReference(version = "1.0.0",group = "urule")
    private HealthService healthService;
    /**
     * 健康检查接口
     */
    @GetMapping("/health")
    public ApiResult<Map<String, Object>> healthCheck() {
        Map<String, Object> health = new HashMap<>();
        try {
            health.put("consumerStatus", "running");
            health.put("serviceInjected", healthService != null);

            // 尝试调用远程服务
            if (healthService != null) {
                String response = healthService.sayHello("健康检查");
                health.put("remoteServiceStatus", "available");
                health.put("testResponse", response);
            } else {
                health.put("remoteServiceStatus", "service not injected");
            }

            return ApiResult.success(health);
        } catch (Exception e) {
            health.put("remoteServiceStatus", "error");
            health.put("error", e.getMessage());
            return ApiResult.fail("健康检查失败: " + e.getMessage());
        }
    }
}
